{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5be2ed77-402d-4c71-9f8f-12d18d066255",
   "metadata": {},
   "source": [
    "# Dummy shower generation and storage\n",
    "\n",
    "This is an example how to generate a random dummy shower and store it in ROOT TTrees.\n",
    "An example with 10 random dummy showers generation and storage can be found in event_generation.py.\n",
    "\n",
    "First, necessary imports:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "baec770b-ebb7-42dd-96cc-9c7406ec0b32",
   "metadata": {},
   "outputs": [],
   "source": [
    "from grand.grandlib_classes.grandlib_classes import *"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13f614a2-5d37-4d4c-a3f4-831b4b84c75c",
   "metadata": {},
   "source": [
    "Create the Event class instance:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c52f79ba-45e5-4a18-aa18-ce295059122a",
   "metadata": {},
   "outputs": [],
   "source": [
    "event = Event()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d146c1ec-b2f7-49e6-b442-d94dfae4e95c",
   "metadata": {},
   "source": [
    "Fill the part of the event that contains the Run information - common for all (or many - to be decided by the user) the events:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "505c74c4-fa0e-425a-a0cf-b8f980b7a870",
   "metadata": {},
   "outputs": [],
   "source": [
    "event.run_number = 0\n",
    "event.event_number = 0\n",
    "event.site = \"dummy site\"\n",
    "event.first_event = 0\n",
    "event.last_event = 0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18cb3b25-0580-4bef-b4c7-822ea02c7a6d",
   "metadata": {},
   "source": [
    "Prepare the empty lists for storing voltage and electric field traces, as well as antenna information for this event:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73fcb58f-75b0-4d22-b7d8-2c61611b1943",
   "metadata": {},
   "outputs": [],
   "source": [
    "event.voltages = []\n",
    "event.antennas = []\n",
    "event.efields = []"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55e1644d-19ff-4612-a600-4a19a84a0d57",
   "metadata": {},
   "source": [
    "Let's have 5 traces in this event:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac12e0e3-f631-4eae-a074-be045b5e39a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "trace_count = 5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7697147-111e-40f2-9802-70a7efdd437d",
   "metadata": {},
   "source": [
    "Now generate each trace and antenna information in a loop:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "05c000a8-e73f-4bf4-8543-b6884d1b238b",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(trace_count):\n",
    "    # The voltage part\n",
    "    v = Voltage()\n",
    "    # The trace length\n",
    "    v.n_points = np.random.randint(900, 1000)\n",
    "    # v.n_points = np.random.randint(9, 10)\n",
    "    v.trace_x = np.random.randint(-200, 201, v.n_points)/100.\n",
    "    v.trace_y = np.random.randint(-200, 201, v.n_points)/100.\n",
    "    v.trace_z = np.random.randint(-200, 201, v.n_points)/100.\n",
    "    event.voltages.append(v)\n",
    "    \n",
    "    # The antenna part\n",
    "    a = Antenna()\n",
    "    a.atm_temperature = np.random.randint(-400, 401)/100.\n",
    "    a.atm_pressure = np.random.randint(9000, 11000)/10.\n",
    "    a.atm_humidity = np.random.rand()*100\n",
    "    a.battery_level = np.random.rand()*100\n",
    "    a.firmware_version = 1\n",
    "    event.antennas.append(a)\n",
    "    \n",
    "    # The efield part\n",
    "    e = Efield()\n",
    "    e.n_points = v.n_points\n",
    "    v2ef = 1.17\n",
    "    e.trace_x = v.trace_x*v2ef\n",
    "    e.trace_y = v.trace_y*v2ef\n",
    "    e.trace_z = v.trace_z*v2ef\n",
    "    event.efields.append(e)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e14d3ba9-aabf-46d9-97af-106da0802144",
   "metadata": {},
   "source": [
    "Fill the shower part:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6c8a85ab-94f8-43b4-a5c0-e003abe9c298",
   "metadata": {},
   "outputs": [],
   "source": [
    "# The shower part\n",
    "event.shower = Shower()\n",
    "event.shower.energy = np.random.rand()\n",
    "## Shower Xmax [g/cm2]\n",
    "event.shower.Xmax = np.random.randint(1000, 4000)/10.\n",
    "## Shower position in the site's reference frame\n",
    "event.shower.Xmaxpos = np.random.rand(3)*1000\n",
    "## Direction of origin (ToDo: is it the same as origin of the coordinate system?)\n",
    "event.shower.origin_geoid = np.zeros(3)\n",
    "## Poistion of the core on the ground in the site's reference frame\n",
    "event.shower.core_ground_pos = np.random.rand(4)*1000"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7abf9da7-5b26-4e1f-aa14-65eb4a145d75",
   "metadata": {},
   "source": [
    "Remove the old output file if it exists (this will be improved):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77b1ac12-f8c6-4ee8-ba32-bdbc2544eecb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os.path\n",
    "\n",
    "if os.path.isfile(\"dummy_random_event.root\"):\n",
    "    os.remove(\"dummy_random_event.root\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37343edc-4241-4a95-aa81-e5af3747e3f0",
   "metadata": {},
   "source": [
    "Write the generated event to HDD to a dummy file:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d41b0d72-c8cb-4250-a799-a47fbe54e1b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "event.write(\"dummy_random_event.root\")\n",
    "print(\"Wrote\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
